#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   generate_log.py    
@Contact :   291622538@qq.com

@Modify Time      @Author    @Version    @Description
------------      -------    --------    -----------
2020/11/16 19:15   fan        1.0         随机生成爱奇艺访问日志
"""

import random
import time

url_paths = ["www/2", "www/1", "www/6", "www/4", "www/3", "pianhua/130", "toukouxu/821"]

ip_slices = [132, 156, 124, 10, 29, 167, 143, 187, 30, 100]

status_code = [404, 302, 200]

http_referers = [
    "https://www.baidu.com/s?wd={query}",
    "https://www.sogou.com/web?qu={query}",
    "http://cn.bing.com/search?q={query}",
    "https://search.yahoo.com/search?p={query}",
]

search_keyword = [
    "猎场",
    "快乐人生",
    "极限挑战",
    "我的体育老师",
    "幸福满院"
]


# 生成ip
def sample_ip():
    return ".".join([str(item) for item in random.sample(ip_slices, 4)])


def sample_url():
    return "\"GET " + random.sample(url_paths, 1)[0] + " HTTP/1.0\""


def sample_status():
    return random.sample(status_code, 1)[0]


def sample_reference():
    if random.uniform(0, 1) > 0.2:
        return "-"
    refer_str = random.sample(http_referers, 1)
    query_str = random.sample(search_keyword, 1)
    return refer_str[0].format(query=query_str[0])


# 生成日志
def generate_log(count=10):
    f = open("data/logs", "a+", encoding="utf-8")
    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    while count >= 1:
        query_log = "%s\t%s\t%s\t%s\t%s" % (sample_ip(), time_str, sample_url(), sample_reference(), sample_status(),)
        print(query_log)
        f.write(query_log + "\n")
        count -= 1


if __name__ == '__main__':
    while True:
        generate_log(100)
        time.sleep(10)
